package com.pktechan.api.vo;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: s.zhangbj03307
 * @create: 2021/4/14 15:50
 **/
public class TradeOrderExpireDelayed implements Delayed {
    /**
     * 订单编号（主键）
     */
    private Integer tradeOrderId;

    /**
     * 过期时间
     */
    private Date expirationTime;

    public Integer getTradeOrderId() {
        return tradeOrderId;
    }

    public void setTradeOrderId(Integer tradeOrderId) {
        this.tradeOrderId = tradeOrderId;
    }

    public Date getExpirationTime() {
        return expirationTime;
    }

    public void setExpirationTime(Date expirationTime) {
        this.expirationTime = expirationTime;
    }

    /**
     * 判断过期的策略：过期时间大于等于当前时间就算过期
     *
     * @param unit
     * @return
     */
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expirationTime.getTime() - System.currentTimeMillis(), TimeUnit.NANOSECONDS);
    }

    /**
     * 订单加入队列的排序规则
     *
     * @param o
     * @return
     */
    @Override
    public int compareTo(Delayed o) {
        TradeOrderExpireDelayed orderDelayDto = (TradeOrderExpireDelayed) o;
        long time = orderDelayDto.getExpirationTime().getTime();
        long time1 = this.getExpirationTime().getTime();
        return time == time1 ? 0 : time < time1 ? 1 : -1;

    }

    /**
     * 重写用于删除
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        TradeOrderExpireDelayed tradeOrderExpireDelayed = (TradeOrderExpireDelayed) obj;
        if (tradeOrderExpireDelayed.getTradeOrderId() == null) {
            return false;
        } else if (!tradeOrderId.equals(tradeOrderExpireDelayed.getTradeOrderId())) {
            return false;
        }
        return true;
    }

}
